<template>
    <div>
        <table-search :searchCol="searchCol">

            <Select v-if="isDeleteShow" slot="partnerId" size="small" v-model="queryObj.partnerId"
                    @on-change="getAllTeamInfo" filterable>
                <Option v-for="(item,index) in partnerComList"
                        v-bind:value="item.id">{{item.partnerName}}</Option>
            </Select>

            <Select slot="teamName" size="small" v-model="queryObj.id" filterable>
                <Option v-for="(item,index) in teamList"
                        v-bind:value="item.id">{{item.teamName}}</Option>
            </Select>

            <Select slot="state" size="small" v-model="queryObj.state">
                <Option v-for="(item,index) in commonstate" v-text="item.name"
                        v-bind:value="item.code"></Option>
            </Select>

            <div slot="button">
                <Button size="small" @click="findData" type="primary">查 询</Button>
                <Button size="small" @click="resetQuery" type="error">重 置</Button>
                <Button size="small" @click="newOrder" type="success" v-if="myRole != 3">新 建</Button>
            </div>

        </table-search>

        <com-table :columns="columns" :list="items" :loading="false">
            <div slot="No" slot-scope="{index}">
                {{(pageNumber - 1) * pageSize + index + 1}}
            </div>
            <div slot="action" slot-scope="{item}">
                <a @click="callDelete(item)" v-if="isDeleteShow">删除</a>

                <a @click="editTeam(item.id, item.partnerId)" v-if="myRole != 3">编辑</a>
            </div>

        </com-table>


        <div class="quote">
            <div class="quoteB">
                <div class="page">
                    <Page style="margin: 0 auto" :total="pageTotal" :current="pageNumber" @on-change="cutPage"
                          @on-page-size-change="cutSize" show-elevator show-total show-sizer></Page>
                </div>
            </div>
        </div>

    </div>

</template>

<script type="text/javascript">
    import api from '../../utils/api.js';
    //  import commonCode from '../../assets/json/common_code.json';
    import {queryROLE, queryPartnerId, queryPartnerName} from '../../utils/common.js';
    import reqDic from '../../utils/requestDictionary.js';
    import TableSearch from "../../../node_modules/aoyun-components/src/components/Table/TableSearch";

    export default {
        components: {TableSearch},
        data: function () {
            return {
                columns: [
                    {key: 'No', title: 'No.', type: 'custom'},
                    {key: 'teamName', title: '团队名称'},
                    {key: 'partnerName', title: '营业部'},
                    {key: 'teamLeaderName', title: '团队长'},
                    {key: 'stateName', title: '状态'},
                    {key: 'updateBy', title: '更新人'},
                    {key: 'updateTime', title: '更新时间'},
                    {key: '__action', title: '操作'}
                ],

                searchCol: [
                    {key: "partnerId", title: "营业部"},
                    {key: "teamName", title: "团队名称"},
                    {key: "state", title: "状态"},
                ],
                pageSize: 10,
                pageNumber: 1,
                pageTotal: 0,

                queryObj: {
                    state: '',
                    id: '',
                    partnerId: ''
                },

                commonstate: [],

                items: [],

                teamId: '',

                partnerId: '',

                isDeleteShow: false,

                //我的角色
                myRole: '',
                //删除按钮
                isHideDelete: false,

                partnerComList: [],

                teamList: []

            }
        },
        created: function () {
            this.pageSize = 10;
            this.pageNumber = 1;
            this.commonstate = reqDic.QueryDictionary("COMMONSTATE");
            this.commonstate.unshift({'code': '', 'name': '请选择'});
        },
        activated: function () {
            let self = this
            this.permissionControl();

            if (this.myRole == 1) {
                this.getPartnerComList();
            } else {
                this.searchCol.forEach(function (item, index) {
                    if (item.key == 'partnerId') {
                        self.searchCol.splice(index, 1)
                    }

                })
                this.queryObj.partnerId = JSON.parse(window.localStorage.operation).partnerId
                this.getAllTeamInfo()
                this.query();
            }
        },
        methods: {

            callDelete: function (item) {

                var self = this;

                var config = {
                    "onOk": function () {
                        self.deleteTeam(item.id);
                    },
                    "content": "确定删除团队【" + item.teamName + "】么",
                    "title": "删除团队"
                }
                this.$Modal.confirm(config);

            },

            //权限控制的方法
            permissionControl: function () {
                this.myRole = queryROLE();
                this.isDeleteShow = this.myRole == 1;
                this.isHideDelete = this.myRole != 1 && this.myRole != 2;

            },

            findData: function () {
                this.pageNumber = 1;
                this.query();

            },
            query: function () {
                var self = this;
                if (this.queryObj.partnerId == '') {
                    delete this.queryObj.partnerId
                }
                if (this.queryObj.id == '') {
                    delete this.queryObj.id
                }
                api.getTeamDefinePageQuery(this.pageSize, this.pageNumber, this.queryObj).then((res) => {
                    let data = res.body.data;
                    data.rows.forEach(function (item) {
                        item.stateName = reqDic.QueryDictionary("COMMONSTATE", item.state, "name");
                    });
                    this.pageTotal = data.total;
                    this.items = data.rows;
                }).catch((res) => {

                })
            },

            resetQuery: function () {
                this.queryObj = {
                    teamName: "",
                    state: ""
                };
            },

            cutPage: function (pageNumber) {
                this.pageNumber = pageNumber;
                this.query();
            },

            cutSize: function (pageSize) {
                this.pageSize = pageSize;
                this.query();
            },

            editTeam: function editTeam(id, partnerId) {
                this.$router.push("buildTeam/" + id + '/' + partnerId);
            },

            deleteTeam: function deleteTeam(id) {
                var self = this;
                api.deleteTeam(id).then(res => {

                    if (res.body.state === "1") {
                        self.$Message.success(res.body.message.info);
                    } else {
                        self.$Message.error(res.body.message.info);
                    }
                    this.query();
                })
            },

            newOrder: function () {
                if(this.isDeleteShow){
                    this.$router.push("buildTeam/-1/-1");
                }else {
                    this.$router.push("buildTeam/-1/"+this.queryObj.partnerId);
                }
            },

            getPartnerComList: function () {
                var self = this;
                api.getPartnerComList().then(res => {
                    self.partnerComList = res.body.data;
                    self.partnerComList.unshift({id: '', partnerName: "请选择"})

                    this.query();
                });
            },

            getAllTeamInfo: function () {
                let self = this
                let data = {
                    partnerId: this.queryObj.partnerId,
                }
                api.getAllTeamInfo(data).then(function (res) {
                    if (res.body.state == 1) {
                        self.queryObj.id = '';
                        self.teamList = res.body.data
                        self.teamList.unshift({id: '', teamName: "请选择"})
                    }
                })
            }
        },
    }
</script>
